%entry point Newton-Gregory
%   Last edited by Samsu Sempena (13507088) 22 Maret 2011, 10.00
function [hasil] = mn_newton_gregory(A,titik_interpolasi,derajat,handles,tipe)
switch (tipe)
    case 'maju'
        hasil = mn_newton_gregory_maju(A,titik_interpolasi,derajat,handles);
    case 'mundur'
        hasil = mn_newton_gregory_mundur(A,titik_interpolasi,derajat,handles);
end

function [hasil] = mn_newton_gregory_maju(A,titik_interpolasi,derajat,handles)

%Interpolasi dengan polinom newton gregory maju
if (ndims(A)==2)          %interpolasi 1 peubah
    x = A(:,1);
    y = A(:,2);
       
    %pembentukan tabel selisih (delta)
    %t = length(A(:,1));
    t = derajat;
    
    for m = 1:t   %kolom pertama delta diisi dengan nilai f(x)
        delta(m,1) = y(m);
    end
    for n = 2:t+1   
        for m = 1:(t-n+1)
            delta(m,n) = delta(m+1,n-1) - delta(m,n-1);
        end                
    end
    
    set(handles.uitable_selisih,'Data',delta);       
    
    %menggambar polinom interpolasi
    for xx=A(1,1):A(length(A(:,1)),1)
        hasil = 0;
        s = (xx - x(1))/(x(2)-x(1));
        for n = 1:t
            temp = delta(1,n);
            for m = 0:(n-2)
                temp = temp * (s-m);
            end
            hasil = hasil + temp/factorial(n-1);
        end
        P(xx-A(1,1)+1,1) = xx;
        P(xx-A(1,1)+1,2) = hasil;
    end
    cla(handles.axes1,'reset');
    plot(handles.axes1,P(:,1),P(:,2),'r');
    hold on;
    plot(handles.axes1,A(:,1),A(:,2),'b*');
    
    %menghitung nilai interpolasi
    xx = titik_interpolasi;
    hasil = 0;
    s = (xx - x(1))/(x(2)-x(1));
    for n = 1:t
        temp = delta(1,n);
        for m = 0:(n-2)
            temp = temp * (s-m);
        end
        hasil = hasil + temp/factorial(n-1);
    end

elseif (ndims(A)==3)      %interpolasi 2 peubah
    msgbox('dua peubah : tidak diimplementasikan');
end

function [hasil] = mn_newton_gregory_mundur(A,titik_interpolasi,derajat,handles)
%Interpolasi dengan polinom newton gregory maju
%   Last edited by Samsu Sempena (13507088) 16 Maret 2011, 17.25

if (ndims(A)==2)          %interpolasi 1 peubah
    x = A(:,1);
    y = A(:,2);
       
    %pembentukan tabel selisih (delta)
    %t = length(A(:,1));
    t = derajat;
    
    for m = 1:t   %kolom pertama delta diisi dengan nilai f(x)
        delta(m,1) = y(m);
    end
    
    for n = 2:t+1   
        for m = n:t
            delta(m,n) = delta(m,n-1) - delta(m-1,n-1);
        end                
    end
    
    set(handles.uitable_selisih,'Data',delta);       
    %menggambar polinom interpolasi
    for xx=A(1,1):A(length(A(:,1)),1)
        hasil = 0;
        s = (xx - x(t))/(x(2)-x(1));
        for n = 1:t
            temp = delta(t,n);
            for m = 0:(n-2)
                temp = temp * (s+m);
            end
            hasil = hasil + temp/factorial(n-1);
        end
        P(xx-A(1,1)+1,1) = xx;
        P(xx-A(1,1)+1,2) = hasil;
    end
    cla(handles.axes1,'reset');
    plot(handles.axes1,P(:,1),P(:,2),'r');
    hold on;
    plot(handles.axes1,A(:,1),A(:,2),'b*');
    
    %menghitung nilai interpolasi
    xx = titik_interpolasi;
    hasil = 0;
    s = (xx - x(t))/(x(2)-x(1));
    for n = 1:t
        temp = delta(t,n);
        for m = 0:(n-2)
            temp = temp * (s+m);
        end
        hasil = hasil + temp/factorial(n-1);
    end

elseif (ndims(A)==3)      %interpolasi 2 peubah
    msgbox('dua peubah : tidak diimplementasikan');
end
